package com.sfh.service.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sfh.service.model.table.FileTable;
import com.sfh.service.util.SQLHelp;

public class FileResModel extends BaseModel<FileResModel> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;
	public static final FileResModel FILE = new FileResModel().dao();
	
	@Override
	public FileResModel findById(Object idValue) {
		return super.findFirst("select * from "+FileTable.Table+" where "+FileTable.id+"=?", idValue);
	}
	
	public long getId() {
		return this.getLong(FileTable.id.toString());
	}

	public void setId(long id) {
		this.set(FileTable.id.toString(), id);
	}
	
	public Date getTime() {
		return this.get(FileTable.time.toString());
	}

	public void setTime(Date date) {
		this.set(FileTable.time.toString(), date);
	}

	public long getVersion() {
		return this.getLong(FileTable.version.toString());
	}
	public void setVersion(long version) {
		this.set(FileTable.version.toString(), version);
	}
	
	public long getUserId() {
		return this.getLong(FileTable.userId.toString());
	}

	public void setUserId(long userId) {
		this.set(FileTable.userId.toString(), userId);
	}
	
	public String getPath() {
		return this.get(FileTable.path.toString());
	}

	public void setPath(String path) {
		this.set(FileTable.path.toString(), path);
	}
	
	public long getRelationId() {
		return this.get(FileTable.relationId.toString());
	}

	public void setRelationId(long relationId) {
		this.set(FileTable.relationId.toString(), relationId);
	}
	

	/***
	 * 获取图片地址
	 * 
	 * @param relationId
	 * @return
	 */
	public String getFileByRelationId(long relationId) {

		SQLHelp sqlHelp = new SQLHelp(FileTable.Table);
		sqlHelp.addWhereEqualTo(FileTable.relationId, relationId);

		FileResModel fileModel = this.findFirst(sqlHelp.build());
		if (fileModel != null) {
			return fileModel.getPath();
		}
		return "";

	}

	/***
	 * 所有图片
	 * @param relationId
	 * @return
	 */
	public List<String> listFileByRelationId(long relationId) {
		
		SQLHelp sqlHelp = new SQLHelp(FileTable.Table);
		sqlHelp.addWhereEqualTo(FileTable.relationId, relationId);
		
		List<FileResModel> list = this.find(sqlHelp.build());
		List<String> path = new ArrayList<String>(9);

		if (list != null) {
			for (FileResModel model : list) {
				path.add(model.getPath());
			}
		}
		return path;

	}
}
